package com.gtw.modules.sys.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.gtw.common.annotation.SysLog;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.entity.TbCommodity;
import com.gtw.modules.entity.TbCommodityProperty;
import com.gtw.modules.sys.DTO.CommodityPropertyDTO;
import com.gtw.modules.sys.service.SysCommodityService;
import com.gtw.modules.sys.service.SysPropertyService;
import com.gtw.modules.entity.TbProperty;
import com.gtw.modules.sys.service.SysTbCommodityPropertyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 商品属性表
 *
 * @author wuye
 * @email www.baidu.com
 * @date 2018-02-01 10:46:46
 */
@RestController
@RequestMapping("/sys/property")
public class SysPropertyController extends AbstractController{

    @Autowired
    private SysPropertyService propertyService;

    @Autowired
    private SysTbCommodityPropertyService tbCommodityPropertyService;

    @Autowired
    private SysCommodityService sysCommodityService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:property:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<TbProperty> pageUtil = new Page<TbProperty>(query.getPage(), query.getLimit());
        Page<TbProperty> page = propertyService.queryPageList(pageUtil, query);
        return R.ok().put("page", page);
    }

    /**
     * 查询所有属性
     * @return
     */
    @RequestMapping("/findAll")
    public R findAll(){
        List<TbProperty> list = propertyService.findaLLProperty();
        return R.ok().put("list",list);
    }

    /**
     * 根据属性名查询属性值
     * @return
     */
    @RequestMapping("/findByName")
    public R findByName(@RequestParam String name){
        List<TbProperty> list = propertyService.findByName(name);
        return R.ok().put("list",list);
    }


    /**
     * 根据商品ID查询
     * @return
     */
    @RequestMapping("/findByCommodityId")
    public R findByCommodityId(@RequestParam Integer commodityid){
        List<TbCommodityProperty> list = tbCommodityPropertyService.foundByCommodityid(commodityid);
        List<CommodityPropertyDTO> listdto = new ArrayList<>();
        for (TbCommodityProperty tcp: list) {
            CommodityPropertyDTO dto = new CommodityPropertyDTO();
            TbCommodity tbCommodity = sysCommodityService.queryObject(tcp.getCommodityid());
            TbProperty tbProperty = propertyService.queryObject(tcp.getPropertyid());
            TbCommodityProperty tbCommodityProperty = tcp;
            dto.setTbCommodity(tbCommodity);
            dto.setTbProperty(tbProperty);
            dto.setTbCommodityProperty(tbCommodityProperty);
            listdto.add(dto);
        }
        return R.ok().put("listdto",listdto);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:property:info")
    public R info(@PathVariable("id") Integer id) {
        TbProperty property = propertyService.queryObject(id);

        return R.ok().put("property", property);
    }

    /**
     * 保存
     */
    @SysLog("添加商品属性")
    @RequestMapping("/save")
    @RequiresPermissions("sys:property:save")
    public R save(@RequestBody TbProperty property) {
        property.setCreatedate(new Date());
        property.setIsdelete(false);
        property.setClassid(0);
        propertyService.save(property);

        return R.ok();
    }

    /**
     * 为商品添加属性
     * @return
     */
    @RequestMapping("/saveCommodityProperty")
    public R saveCommodityProperty(@RequestParam Integer propertyId,@RequestParam Integer commodityId,@RequestParam Integer num){
        TbCommodityProperty tbCommodityProperty = new TbCommodityProperty();
        tbCommodityProperty.setPropertynum(num);
        tbCommodityProperty.setPropertyid(propertyId);
        tbCommodityProperty.setCommodityid(commodityId);
        tbCommodityProperty.setCreateuser(getUser().getUsername());
        tbCommodityProperty.setCreatedate(new Date());
        tbCommodityProperty.setUpdatedate(new Date());
        tbCommodityProperty.setUpdateuser(getUser().getUsername());
        tbCommodityProperty.setIsdelete(false);
        tbCommodityPropertyService.save(tbCommodityProperty);
        return R.ok();
    }

    /**
     * 为商品修改属性
     * @return
     */
    @RequestMapping("/updateCommodityProperty")
    public R updateCommodityProperty(@RequestParam Integer id,@RequestParam Integer propertyId,@RequestParam Integer commodityId,@RequestParam Integer num){
        TbCommodityProperty tbCommodityProperty = tbCommodityPropertyService.queryObject(id);
        tbCommodityProperty.setPropertynum(num);
        tbCommodityProperty.setPropertyid(propertyId);
        tbCommodityProperty.setCommodityid(commodityId);
        tbCommodityProperty.setUpdatedate(new Date());
        tbCommodityProperty.setUpdateuser(getUser().getUsername());
        tbCommodityPropertyService.update(tbCommodityProperty);
        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改商品属性")
    @RequestMapping("/update")
    @RequiresPermissions("sys:property:update")
    public R update(@RequestBody TbProperty property) {
        propertyService.update(property);
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除商品属性")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:property:delete")
    public R delete(@RequestBody Integer[] ids) {
        propertyService.deleteBatch(ids);
        return R.ok();
    }

}
